/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * @author yaoyuming
 * @date  2020-04-02 11:14
 */
package com.yao.home;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author yaoyuming
 * @date 2020/04/02 11:14
 */
@Service
@Slf4j
public class AsyncServiceImpl implements  AsyncService{

    private List<Integer> intList = new LinkedList<>();

    @Async
    @Override
    public void read() throws InterruptedException {
        while (true) {
            TimeUnit.SECONDS.sleep(1L);
            if (intList.isEmpty()) {
                log.info("我想要读取list，但是它目前为空，等待...");
            } else {
                log.info("读取第[0]个元素为：{}", intList.get(0));
                intList.remove(0);
            }
        }

    }

    @Async
    @Override
    public void write() throws InterruptedException {
        while (true) {
            TimeUnit.SECONDS.sleep(2L);
            if (intList.size() < 10) {
                int i = RandomUtil.randomInt(10);
                log.info("写入随机数字:{} , 当前列表个数：{}",i, intList.size());
                intList.add(i);
            } else {
                log.info(Thread.currentThread().getName() + "我想要写入，但是它目前为个数为{}个", intList.size());
            }
        }
    }
}